package com.llap.exp190929.controller;

import com.llap.exp190929.dto.PaginatedDto;
import com.llap.exp190929.entity.Blog;
import com.llap.exp190929.entity.User;
import com.llap.exp190929.error.ForbiddenException;
import com.llap.exp190929.error.RestException;
import com.llap.exp190929.error.UnauthorizedException;
import com.llap.exp190929.security.JwtUser;
import com.llap.exp190929.service.BlogService;
import com.llap.exp190929.service.UserService;
import com.llap.exp190929.util.Util;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@RestController
@RequestMapping("blogs")
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class BlogsController {
    private BlogService blogService;

    @GetMapping("")
    public PaginatedDto<Blog> findBlogs(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize) throws RestException {
        User user = User.from(Util.getLoginUser());
        if (user == null) throw new UnauthorizedException("");
        return PaginatedDto.from(blogService.findBlogs(page, pageSize, user));
    }

    @GetMapping("{id}")
    public Blog findBlog(@PathVariable Integer id) {
        return blogService.findBlog(id);
    }

    @PostMapping("")
    public Blog addBlog(@RequestBody Map<String, String> data) throws RestException {
        Blog blog = new Blog();
        User user = User.from(Util.getLoginUser());
        String content = data.get("content");
        if (user == null) throw new UnauthorizedException("");
        if (content == null || content.isEmpty()) throw new ForbiddenException("Content is empty");
        blog.setUser(user);
        blog.setContent(content);
        blog.setPublishTime(new Date());
        return blogService.addBlog(blog);
    }

    @DeleteMapping("")
    public void deleteBlog(@RequestParam Integer id) throws RestException {
        System.out.println(id);
        Blog blog = blogService.findBlog(id);
        if (blog != null) {
            if (Util.isAdmin(Util.getLoginUser()) || blog.getUser().is(User.from(Util.getLoginUser()))) {
                blogService.deleteBlog(id);
            }
            else throw new UnauthorizedException("Cannot delete blog");
        }
        else throw new ForbiddenException("Blog not found");
    }

    @PatchMapping("")
    public Blog updateBlog(@RequestBody Blog blog) throws RestException {
        if (blog != null && blog.getContent() != null && !blog.getContent().isEmpty()) {
            if (Util.isAdmin(Util.getLoginUser()) || blog.getUser().is(User.from(Util.getLoginUser()))) {
                return blogService.updateBlog(blog);
            }
             else throw new UnauthorizedException("Cannot update blog");
        }
        else throw new ForbiddenException("Blog cannot be empty");
    }
}
